package com.ruoyi.quotation.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quotation.domain.QuoPricing;
import com.ruoyi.quotation.service.IQuoPricingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 成本核算Controller
 */
@RestController
@RequestMapping("/quotation/pricing")
public class QuoPricingController extends BaseController {

    @Autowired
    private IQuoPricingService quoPricingService;

    /**
     * 查询成本核算列表
     */
    @PreAuthorize("@ss.hasPermi('quotation:pricing:list')")
    @GetMapping("/list")
    public TableDataInfo list(QuoPricing quoPricing) {
        startPage();
        List<QuoPricing> list = quoPricingService.selectQuoPricingList(quoPricing);
        return getDataTable(list);
    }

    /**
     * 导出成本核算列表
     */
    @PreAuthorize("@ss.hasPermi('quotation:pricing:export')")
    @Log(title = "成本核算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuoPricing quoPricing) {
        List<QuoPricing> list = quoPricingService.selectQuoPricingList(quoPricing);
        ExcelUtil<QuoPricing> util = new ExcelUtil<>(QuoPricing.class);
        util.exportExcel(response, list, "成本核算数据");
    }

    /**
     * 获取成本核算详细信息
     */
    @PreAuthorize("@ss.hasPermi('quotation:pricing:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(quoPricingService.selectQuoPricingById(id));
    }
    
    /**
     * 根据订单编号获取成本核算信息
     */
    @PreAuthorize("@ss.hasPermi('quotation:pricing:query')")
    @GetMapping(value = "/byOrderNo/{orderNo}")
    public AjaxResult getInfoByOrderNo(@PathVariable("orderNo") String orderNo) {
        return success(quoPricingService.selectQuoPricingByOrderNo(orderNo));
    }

    /**
     * 新增成本核算
     */
    @PreAuthorize("@ss.hasPermi('quotation:pricing:add')")
    @Log(title = "成本核算", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuoPricing quoPricing) {
        return toAjax(quoPricingService.insertQuoPricing(quoPricing));
    }

    /**
     * 修改成本核算
     */
    @PreAuthorize("@ss.hasPermi('quotation:pricing:edit')")
    @Log(title = "成本核算", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuoPricing quoPricing) {
        return toAjax(quoPricingService.updateQuoPricing(quoPricing));
    }

    /**
     * 删除成本核算
     */
    @PreAuthorize("@ss.hasPermi('quotation:pricing:remove')")
    @Log(title = "成本核算", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(quoPricingService.deleteQuoPricingByIds(ids));
    }
    
    /**
     * 执行财务审核
     */
    @PreAuthorize("@ss.hasPermi('quotation:pricing:audit')")
    @Log(title = "财务审核", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody QuoPricing quoPricing) {
        return toAjax(quoPricingService.auditPricing(quoPricing));
    }
    
    /**
     * 财务反审核
     */
    @PreAuthorize("@ss.hasPermi('quotation:pricing:reaudit')")
    @Log(title = "财务反审核", businessType = BusinessType.UPDATE)
    @PostMapping("/reaudit")
    public AjaxResult reaudit(@RequestBody QuoPricing quoPricing) {
        return toAjax(quoPricingService.reauditPricing(quoPricing));
    }
    
    /**
     * 计算价格
     */
    @PreAuthorize("@ss.hasPermi('quotation:pricing:calculate')")
    @GetMapping("/calculate")
    public AjaxResult calculate(QuoPricing quoPricing) {
        return success(quoPricingService.calculatePrice(quoPricing));
    }
} 